<?php

class TimerController extends ControllerBase
{
    public function initialize()
    {
        $this->tag->setTitle('时间');
        parent::initialize();
    }

    public function indexAction()
    {
        
    }


    public function getBeginTimeAction()
    {
        if($this->request->isGet()){
            $id = $this->request->getQuery("id", null, 0);
    	    echo $this->getBeginTime($id);
        }else{
            echo 'error';
        }
    }

    public function getEndTimeAction()
    {
        if($this->request->isGet()){
            $id = $this->request->getQuery("id", null, 0);
            echo $this->getEndTime($id);
        }else{
            echo 'error';
        }
    }

    public function getBeginTime($id)
    {
        if($id != 0 && is_numeric($id)){
            $test = Tests::findFirst($id);

            $currentTime = time();
            $endTime = strtotime($test->getBeginDate());
            return $endTime - $currentTime;
        }
        return 'error';
    }

    public function getEndTime($id)
    {
        if($id != 0 && is_numeric($id)){
            $test = Tests::findFirst($id);

            $currentTime = time();
             $endTime = strtotime($test->getEndDate());
            return $endTime - $currentTime;
        }
        return 'error';
    }
}